﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.ServiceModel.DomainServices.Client;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using NetBay.Core.BusinessEntities;
using NetBay.Core.BusinessEntities.Barcodes;
using NetBay.Core.Silverlight.Tools;
using Rio.Applications.Web.Services;
using Rio.Framework.Localisation;
using Rio.Framework.Pages;
using Rio.Manager.Application.Model;
using Rio.Manager.Application.ViewModel.BaseClass;

namespace Rio.Manager.Application.ViewModel.Administration
{
    public class BarCodeAddTabViewModel : TabViewModelBase
    {
        #region Fields
        public RelayCommand InitializationCommand { get; set; }
        public RelayCommand AddRowCommand { get; set; }
        public RelayCommand UpdateBarCodeCommand { get; set; }
        public RelayCommand SaveBarCodeCommand { get; set; }
        public RelayCommand CloseBarCodeCommand { get; set; }

        private Boolean _IsModifyMode;
        private ObservableCollection<Barcode> _BarCodeInformationToUpdate;
        private string _BarCodeTitleAdd;
        private string _BarCodeGeneralInformationAddTitle;
        private string _BarCodeIdentifierAddTitle;
        private string _BarCodeIdentifierAdd;
        private string _BarCodeNameAddTitle;
        private string _BarCodeNameAdd;
        private string _BarCodeDescriptionAddTitle;
        private string _BarCodeDescriptionAdd;
        private Boolean _ChbActiveBarCodeIsChecked;
        private string _ChbActivateBarCodeLabel;
        private string _BarCodeTypologyAddTitle;
        private string _BarCodeTypologyTypeAddTitle;
        private ObservableCollection<DisplayBarCodeType> _BarCodeTypologyTypeList;
        private DisplayBarCodeType _SelectedDisplayBarCodeTypologyType;
        private string _BarCodeTypologyPrefixAddTitle;
        private string _BarCodeTypologyPrefixAdd;
        private string _BarCodeFunctionAddTitle;
        private ObservableCollection<DisplayBarCodeFunction> _BarCodeFunctionNameList;
        private DisplayBarCodeFunction _SelectedDisplayBarCodeFunction;
        private Boolean _IsEnabledcbFunctionName;
        private string _ChbModifyFunctionBarCodeLabel;
        private Boolean _ChbModifyFunctionBarCodeIsChecked;
        private Boolean _BarcodeFunctionFolderSeparatorVisible;
        private Boolean _BarcodeFunctionDocumentSeparatorVisible;
        private string _BarCodeSelectedFunctionAddTitle;
        private string _ChbFolderCorrespondenceLabel;
        private string _ChbDeleteBarCodePageLabel;
        private string _ChbManageErrorLabel;
        private string _ChbLimitLabel;
        private Boolean _ChbFolderCorrespondenceIsChecked;
        private Boolean _ChbDeleteBarCodePageIsChecked;
        private Boolean _ChbManageErrorIsChecked;
        private Boolean _ChbLimitIsChecked;
        private Boolean _FolderCorrespondenceVisible;
        private string _OrganizationLabel;
        private string _FolderTypeLabel;
        private string _BarCodeSingleIndexAddTitle;
        private ObservableCollection<DisplayBarCodeSingleIndex> _BarCodeSingleIndexList;
        private DisplayBarCodeSingleIndex _SelectedDisplayBarCodeSingleIndex;
        private string _BarcodeStartCharacterSingleIndexAddTitle;
        private int _BarcodeStartCharacterSingleIndex;
        private string _BarcodeEndCharacterSingleIndexAddTitle;
        private int _BarcodeEndCharacterSingleIndex;
        private Boolean _FolderLimitVisible;
        private string _BtnRadDocumentLimitLabel;
        private string _BtnRadPageLimitLabel;
        private Boolean _BtnRadDocumentLimitIsChecked;
        private Boolean _BtnRadPageLimitIsChecked;
        private string _BarcodeLowerBoundLimitAddTitle;
        private int _BarcodeLowerBoundLimit;
        private string _BarcodeUpeprBoundLimitAddTitle;
        private int _BarcodeUpperBoundLimit;
        private ObservableCollection<BarcodeDocumentSeparator> _BarCodeSeparatorDocumentParametersList;
        private Boolean _IsEditingRadGridViewBarcoDeSeparatorDocumentParametersAdd;
        private string _HeaderBarCodeSeparatorDocumentId;
        private string _HeaderBarCodeSeparatorDocumentNatureName;
        private string _HeaderBarCodeSeparatorDocumentChooseNatureName;
        private string _HeaderBarCodeSeparatorDocumentItemName;
        private string _HeaderBarCodeSeparatorDocumentPdfMerge;
        private string _HeaderBarCodeSeparatorDocumentDeleteBarCodePage;
        private string _HeaderBarCodeSeparatorDocumentManageError;
        private string _HeaderBarCodeSeparatorDocumentInfLimit;
        private string _HeaderBarCodeSeparatorDocumentSupLimit;
        private Boolean _IsEnabledParameterSingleIndexGroupControl;
        private string _Button_Delete_Barcode_Document_Separator_Parameter;
        private string _Button_Validate_Barcode_Document_Separator_Parameter;
        private ObservableCollection<DisplayBarCodeItem> _BarCodeItemList;
        private DisplayBarCodeItem _SelectedDisplayBarCodeItem;
        private string _Button_Save_BarCode;
        private Boolean _IsEnabledAddBarCodeButton;
        private string _Button_Update_BarCode;
        private Boolean _IsEnabledUpdateBarCodeButton;
        private string _Button_Close_BarCode;
        private Boolean _CanInsertRowGridSepDoc; 

        #endregion Fields

        #region Properties

        public Boolean IsModifyMode
        {
            get
            {
                return _IsModifyMode;
            }
            set
            {
                _IsModifyMode = value;
                RaisePropertyChanged("IsModifyMode");
            }
        }

        public ObservableCollection<Barcode> BarCodeInformationToUpdate
        {
            get
            {
                return _BarCodeInformationToUpdate;
            }
            set
            {
                _BarCodeInformationToUpdate = value;
                RaisePropertyChanged("BarCodeInformationToUpdate");
            }
        }

        public string BarCodeTitleAdd
        {
            get
            {
                return _BarCodeTitleAdd;
            }
            set
            {
                _BarCodeTitleAdd = value;
                RaisePropertyChanged("BarCodeTitleAdd");
            }
        }

        public string BarCodeGeneralInformationAddTitle
        {
            get
            {
                return _BarCodeGeneralInformationAddTitle;
            }
            set
            {
                _BarCodeGeneralInformationAddTitle = value;
                RaisePropertyChanged("BarCodeGeneralInformationAddTitle");
            }
        }

        public string BarCodeIdentifierAddTitle
        {
            get
            {
                return _BarCodeIdentifierAddTitle;
            }
            set
            {
                _BarCodeIdentifierAddTitle = value;
                RaisePropertyChanged("BarCodeIdentifierAddTitle");
            }
        }

        public string BarCodeIdentifierAdd
        {
            get
            {
                return _BarCodeIdentifierAdd;
            }
            set
            {
                _BarCodeIdentifierAdd = value;
                RaisePropertyChanged("BarCodeIdentifierAdd");
            }
        }

        public string BarCodeNameAddTitle
        {
            get
            {
                return _BarCodeNameAddTitle;
            }
            set
            {
                _BarCodeNameAddTitle = value;
                RaisePropertyChanged("BarCodeNameAddTitle");
            }
        }

        public string BarCodeNameAdd
        {
            get
            {
                return _BarCodeNameAdd;
            }
            set
            {
                _BarCodeNameAdd = value;
                RaisePropertyChanged("BarCodeNameAdd");
            }
        }

        public string BarCodeDescriptionAddTitle
        {
            get
            {
                return _BarCodeDescriptionAddTitle;
            }
            set
            {
                _BarCodeDescriptionAddTitle = value;
                RaisePropertyChanged("BarCodeDescriptionAddTitle");
            }
        }

        public string BarCodeDescriptionAdd
        {
            get
            {
                return _BarCodeDescriptionAdd;
            }
            set
            {
                _BarCodeDescriptionAdd = value;
                RaisePropertyChanged("BarCodeDescriptionAdd");
            }
        }

        public Boolean ChbActiveBarCodeIsChecked
        {
            get
            {
                return _ChbActiveBarCodeIsChecked;
            }
            set
            {
                _ChbActiveBarCodeIsChecked = value;
                RaisePropertyChanged("ChbActiveBarCodeIsChecked");
            }
        }

        public string ChbActivateBarCodeLabel
        {
            get
            {
                return _ChbActivateBarCodeLabel;
            }
            set
            {
                _ChbActivateBarCodeLabel = value;
                RaisePropertyChanged("ChbActivateBarCodeLabel");
            }
        }

        public string BarCodeTypologyAddTitle
        {
            get
            {
                return _BarCodeTypologyAddTitle;
            }
            set
            {
                _BarCodeTypologyAddTitle = value;
                RaisePropertyChanged("BarCodeTypologyAddTitle");
            }
        }

        public string BarCodeTypologyTypeAddTitle
        {
            get
            {
                return _BarCodeTypologyTypeAddTitle;
            }
            set
            {
                _BarCodeTypologyTypeAddTitle = value;
                RaisePropertyChanged("BarCodeTypologyTypeAddTitle");
            }
        }

        public ObservableCollection<DisplayBarCodeType> BarCodeTypologyTypeList
        {
            get
            {
                return _BarCodeTypologyTypeList;
            }
            set
            {
                _BarCodeTypologyTypeList = value;
                RaisePropertyChanged("BarCodeTypologyTypeList");
            }
        }

        public DisplayBarCodeType SelectedDisplayBarCodeTypologyType
        {
            get
            {
                return _SelectedDisplayBarCodeTypologyType;
            }
            set
            {
                _SelectedDisplayBarCodeTypologyType = value;
                RaisePropertyChanged("SelectedDisplayBarCodeTypologyType");
            }
        }

        public string BarCodeTypologyPrefixAddTitle
        {
            get
            {
                return _BarCodeTypologyPrefixAddTitle;
            }
            set
            {
                _BarCodeTypologyPrefixAddTitle = value;
                RaisePropertyChanged("BarCodeTypologyPrefixAddTitle");
            }
        }

        public string BarCodeTypologyPrefixAdd
        {
            get
            {
                return _BarCodeTypologyPrefixAdd;
            }
            set
            {
                _BarCodeTypologyPrefixAdd = value;
                RaisePropertyChanged("BarCodeTypologyPrefixAdd");
            }
        }

        public string BarCodeFunctionAddTitle
        {
            get
            {
                return _BarCodeFunctionAddTitle;
            }
            set
            {
                _BarCodeFunctionAddTitle = value;
                RaisePropertyChanged("BarCodeFunctionAddTitle");
            }
        }

        public ObservableCollection<DisplayBarCodeFunction> BarCodeFunctionNameList
        {
            get
            {
                return _BarCodeFunctionNameList;
            }
            set
            {
                _BarCodeFunctionNameList = value;
                RaisePropertyChanged("BarCodeFunctionNameList");
            }
        }

        public DisplayBarCodeFunction SelectedDisplayBarCodeFunction
        {
            get
            {
                return _SelectedDisplayBarCodeFunction;
            }
            set
            {
                _SelectedDisplayBarCodeFunction = value;
                RaisePropertyChanged("SelectedDisplayBarCodeFunction");

                //Shows barcode function selected details.            
                BarCodeFillParametersFunctionSelected();

                //Must to be not enabled.
                IsEnabledcbFunctionName = false;
                ChbModifyFunctionBarCodeIsChecked = false;
            }
        }

        public Boolean IsEnabledcbFunctionName
        {
            get
            {
                return _IsEnabledcbFunctionName;
            }
            set
            {
                _IsEnabledcbFunctionName = value;
                RaisePropertyChanged("IsEnabledcbFunctionName");
            }
        }

        public Boolean ChbModifyFunctionBarCodeIsChecked
        {
            get
            {
                return _ChbModifyFunctionBarCodeIsChecked;
            }
            set
            {
                _ChbModifyFunctionBarCodeIsChecked = value;
                RaisePropertyChanged("ChbModifyFunctionBarCodeIsChecked");
            }
        }

        public string ChbModifyFunctionBarCodeLabel
        {
            get
            {
                return _ChbModifyFunctionBarCodeLabel;
            }
            set
            {
                _ChbModifyFunctionBarCodeLabel = value;
                RaisePropertyChanged("ChbModifyFunctionBarCodeLabel");
            }
        }

        public Boolean BarcodeFunctionFolderSeparatorVisible
        {
            get
            {
                return _BarcodeFunctionFolderSeparatorVisible;
            }
            set
            {
                _BarcodeFunctionFolderSeparatorVisible = value;
                RaisePropertyChanged("BarcodeFunctionFolderSeparatorVisible");
            }
        }

        public Boolean BarcodeFunctionDocumentSeparatorVisible
        {
            get
            {
                return _BarcodeFunctionDocumentSeparatorVisible;
            }
            set
            {
                _BarcodeFunctionDocumentSeparatorVisible = value;
                RaisePropertyChanged("BarcodeFunctionDocumentSeparatorVisible");

                //Initialize FolderType and Organization.
                if (!IsModifyMode)
                {
                    SelectedFolderType = null;
                    SelectedOrganization = null;
                }
            }
        }

        public string BarCodeSelectedFunctionAddTitle
        {
            get
            {
                return _BarCodeSelectedFunctionAddTitle;
            }
            set
            {
                _BarCodeSelectedFunctionAddTitle = value;
                RaisePropertyChanged("BarCodeSelectedFunctionAddTitle");
            }
        }

        public string ChbFolderCorrespondenceLabel
        {
            get
            {
                return _ChbFolderCorrespondenceLabel;
            }
            set
            {
                _ChbFolderCorrespondenceLabel = value;
                RaisePropertyChanged("ChbFolderCorrespondenceLabel");
            }
        }

        public string ChbDeleteBarCodePageLabel
        {
            get
            {
                return _ChbDeleteBarCodePageLabel;
            }
            set
            {
                _ChbDeleteBarCodePageLabel = value;
                RaisePropertyChanged("ChbDeleteBarCodePageLabel");
            }
        }

        public string ChbManageErrorLabel
        {
            get
            {
                return _ChbManageErrorLabel;
            }
            set
            {
                _ChbManageErrorLabel = value;
                RaisePropertyChanged("ChbManageErrorLabel");
            }
        }

        public string ChbLimitLabel
        {
            get
            {
                return _ChbLimitLabel;
            }
            set
            {
                _ChbLimitLabel = value;
                RaisePropertyChanged("ChbLimitLabel");
            }
        }

        public Boolean ChbFolderCorrespondenceIsChecked
        {
            get
            {
                return _ChbFolderCorrespondenceIsChecked;
            }
            set
            {
                _ChbFolderCorrespondenceIsChecked = value;
                RaisePropertyChanged("ChbFolderCorrespondenceIsChecked");

                //Show correspondence folder 'panel'.               
                FolderCorrespondenceVisible = ChbFolderCorrespondenceIsChecked;

                //Initialize FolderType, Organization and indexes.
                InitsFolderCorrespondencePartProperties();
            }
        }

        public Boolean ChbDeleteBarCodePageIsChecked
        {
            get
            {
                return _ChbDeleteBarCodePageIsChecked;
            }
            set
            {
                _ChbDeleteBarCodePageIsChecked = value;
                RaisePropertyChanged("ChbDeleteBarCodePageIsChecked");
            }
        }

        public Boolean ChbManageErrorIsChecked
        {
            get
            {
                return _ChbManageErrorIsChecked;
            }
            set
            {
                _ChbManageErrorIsChecked = value;
                RaisePropertyChanged("ChbManageErrorIsChecked");
            }
        }

        public Boolean ChbLimitIsChecked
        {
            get
            {
                return _ChbLimitIsChecked;
            }
            set
            {
                _ChbLimitIsChecked = value;
                RaisePropertyChanged("ChbLimitIsChecked");

                //Show correspondence folder 'panel'.               
                FolderLimitVisible = ChbLimitIsChecked;

                //Initialize limit folder part.
                InitsLimitFolderPartProperties();
            }
        }

        public Boolean FolderCorrespondenceVisible
        {
            get
            {
                return _FolderCorrespondenceVisible;
            }
            set
            {
                _FolderCorrespondenceVisible = value;
                RaisePropertyChanged("FolderCorrespondenceVisible");
            }
        }

        public override Organization SelectedOrganization
        {
            get
            {
                return base.SelectedOrganization;
            }
            set
            {
                base.SelectedOrganization = value;
                RaisePropertyChanged("SelectedOrganization");
            }
        }

        public override FolderType SelectedFolderType
        {
            get
            {
                return base.SelectedFolderType;
            }
            set
            {
                base.SelectedFolderType = value;
                RaisePropertyChanged("SelectedFolderType");

                if (value != null && value.Identifier > 0 && SelectedDisplayBarCodeFunction.Value == BarcodeSeparatorType.FolderSeparator)
                {
                    FillFolderTypeSingleIndexes();
                }
                if (value != null && value.Identifier > 0 && SelectedDisplayBarCodeFunction.Value == BarcodeSeparatorType.DocumentSeparator)
                {
                    //FillOrNotFillBarCodeFolderTypeItem(); In case message
                    FillBarCodeFolderTypeItem();
                }
            }
        }

        public ObservableCollection<DisplayBarCodeSingleIndex> BarCodeSingleIndexList
        {
            get
            {
                return _BarCodeSingleIndexList;
            }
            set
            {
                _BarCodeSingleIndexList = value;
                RaisePropertyChanged("BarCodeSingleIndexList");
            }
        }

        public DisplayBarCodeSingleIndex SelectedDisplayBarCodeSingleIndex
        {
            get
            {
                return _SelectedDisplayBarCodeSingleIndex;
            }
            set
            {
                _SelectedDisplayBarCodeSingleIndex = value;
                RaisePropertyChanged("SelectedDisplayBarCodeSingleIndex");
            }
        }

        public string OrganizationLabel
        {
            get
            {
                return _OrganizationLabel;
            }
            set
            {
                _OrganizationLabel = value;
                RaisePropertyChanged("OrganizationLabel");
            }
        }

        public string FolderTypeLabel
        {
            get
            {
                return _FolderTypeLabel;
            }
            set
            {
                _FolderTypeLabel = value;
                RaisePropertyChanged("FolderTypeLabel");
            }
        }

        public Boolean IsEnabledParameterSingleIndexGroupControl
        {
            get
            {
                return _IsEnabledParameterSingleIndexGroupControl;
            }
            set
            {
                _IsEnabledParameterSingleIndexGroupControl = value;
                RaisePropertyChanged("IsEnabledParameterSingleIndexGroupControl");
            }
        }

        public string BarCodeSingleIndexAddTitle
        {
            get
            {
                return _BarCodeSingleIndexAddTitle;
            }
            set
            {
                _BarCodeSingleIndexAddTitle = value;
                RaisePropertyChanged("BarCodeSingleIndexAddTitle");
            }
        }

        public string BarcodeStartCharacterSingleIndexAddTitle
        {
            get
            {
                return _BarcodeStartCharacterSingleIndexAddTitle;
            }
            set
            {
                _BarcodeStartCharacterSingleIndexAddTitle = value;
                RaisePropertyChanged("BarcodeStartCharacterSingleIndexAddTitle");
            }
        }

        public int BarcodeStartCharacterSingleIndex
        {
            get
            {
                return _BarcodeStartCharacterSingleIndex;
            }
            set
            {
                _BarcodeStartCharacterSingleIndex = value;
                RaisePropertyChanged("BarcodeStartCharacterSingleIndex");
            }
        }

        public string BarcodeEndCharacterSingleIndexAddTitle
        {
            get
            {
                return _BarcodeEndCharacterSingleIndexAddTitle;
            }
            set
            {
                _BarcodeEndCharacterSingleIndexAddTitle = value;
                RaisePropertyChanged("BarcodeEndCharacterSingleIndexAddTitle");
            }
        }

        public int BarcodeEndCharacterSingleIndex
        {
            get
            {
                return _BarcodeEndCharacterSingleIndex;
            }
            set
            {
                _BarcodeEndCharacterSingleIndex = value;
                RaisePropertyChanged("BarcodeEndCharacterSingleIndex");
            }
        }

        public Boolean FolderLimitVisible
        {
            get
            {
                return _FolderLimitVisible;
            }
            set
            {
                _FolderLimitVisible = value;
                RaisePropertyChanged("FolderLimitVisible");
            }
        }

        public string BtnRadDocumentLimitLabel
        {
            get
            {
                return _BtnRadDocumentLimitLabel;
            }
            set
            {
                _BtnRadDocumentLimitLabel = value;
                RaisePropertyChanged("BtnRadDocumentLimitLabel");
            }
        }

        public string BtnRadPageLimitLabel
        {
            get
            {
                return _BtnRadPageLimitLabel;
            }
            set
            {
                _BtnRadPageLimitLabel = value;
                RaisePropertyChanged("BtnRadPageLimitLabel");
            }
        }

        public Boolean BtnRadDocumentLimitIsChecked
        {
            get
            {
                return _BtnRadDocumentLimitIsChecked;
            }
            set
            {
                _BtnRadDocumentLimitIsChecked = value;
                RaisePropertyChanged("BtnRadDocumentLimitIsChecked");
            }
        }

        public Boolean BtnRadPageLimitIsChecked
        {
            get
            {
                return _BtnRadPageLimitIsChecked;
            }
            set
            {
                _BtnRadPageLimitIsChecked = value;
                RaisePropertyChanged("BtnRadPageLimitIsChecked");
            }
        }

        public string BarcodeLowerBoundLimitAddTitle
        {
            get
            {
                return _BarcodeLowerBoundLimitAddTitle;
            }
            set
            {
                _BarcodeLowerBoundLimitAddTitle = value;
                RaisePropertyChanged("BarcodeLowerBoundLimitAddTitle");
            }
        }

        public int BarcodeLowerBoundLimit
        {
            get
            {
                return _BarcodeLowerBoundLimit;
            }
            set
            {
                _BarcodeLowerBoundLimit = value;
                RaisePropertyChanged("BarcodeLowerBoundLimit");
            }
        }

        public string BarcodeUpeprBoundLimitAddTitle
        {
            get
            {
                return _BarcodeUpeprBoundLimitAddTitle;
            }
            set
            {
                _BarcodeUpeprBoundLimitAddTitle = value;
                RaisePropertyChanged("BarcodeUpeprBoundLimitAddTitle");
            }
        }

        public int BarcodeUpperBoundLimit
        {
            get
            {
                return _BarcodeUpperBoundLimit;
            }
            set
            {
                _BarcodeUpperBoundLimit = value;
                RaisePropertyChanged("BarcodeUpperBoundLimit");
            }
        }

        public ObservableCollection<BarcodeDocumentSeparator> BarCodeSeparatorDocumentParametersList
        {
            get
            {
                return _BarCodeSeparatorDocumentParametersList;
            }
            set
            {
                _BarCodeSeparatorDocumentParametersList = value;
                RaisePropertyChanged("BarCodeSeparatorDocumentParametersList");
            }
        }

        public Boolean IsEditingRadGridViewBarcoDeSeparatorDocumentParametersAdd
        {
            get
            {
                return _IsEditingRadGridViewBarcoDeSeparatorDocumentParametersAdd;
            }
            set
            {
                _IsEditingRadGridViewBarcoDeSeparatorDocumentParametersAdd = value;
                RaisePropertyChanged("IsEditingRadGridViewBarcoDeSeparatorDocumentParametersAdd");
            }
        }

        public string HeaderBarCodeSeparatorDocumentId
        {
            get
            {
                return _HeaderBarCodeSeparatorDocumentId;
            }
            set
            {
                _HeaderBarCodeSeparatorDocumentId = value;
                RaisePropertyChanged("HeaderBarCodeSeparatorDocumentId");
            }
        }

        public string HeaderBarCodeSeparatorDocumentNatureName
        {
            get
            {
                return _HeaderBarCodeSeparatorDocumentNatureName;
            }
            set
            {
                _HeaderBarCodeSeparatorDocumentNatureName = value;
                RaisePropertyChanged("HeaderBarCodeSeparatorDocumentNatureName");
            }
        }

        public string HeaderBarCodeSeparatorDocumentChooseNatureName
        {
            get
            {
                return _HeaderBarCodeSeparatorDocumentChooseNatureName;
            }
            set
            {
                _HeaderBarCodeSeparatorDocumentChooseNatureName = value;
                RaisePropertyChanged("HeaderBarCodeSeparatorDocumentChooseNatureName");
            }
        }

        public string HeaderBarCodeSeparatorDocumentItemName
        {
            get
            {
                return _HeaderBarCodeSeparatorDocumentItemName;
            }
            set
            {
                _HeaderBarCodeSeparatorDocumentItemName = value;
                RaisePropertyChanged("HeaderBarCodeSeparatorDocumentItemName");
            }
        }

        public string HeaderBarCodeSeparatorDocumentPdfMerge
        {
            get
            {
                return _HeaderBarCodeSeparatorDocumentPdfMerge;
            }
            set
            {
                _HeaderBarCodeSeparatorDocumentPdfMerge = value;
                RaisePropertyChanged("HeaderBarCodeSeparatorDocumentPdfMerge");
            }
        }

        public string HeaderBarCodeSeparatorDocumentDeleteBarCodePage
        {
            get
            {
                return _HeaderBarCodeSeparatorDocumentDeleteBarCodePage;
            }
            set
            {
                _HeaderBarCodeSeparatorDocumentDeleteBarCodePage = value;
                RaisePropertyChanged("HeaderBarCodeSeparatorDocumentDeleteBarCodePage");
            }
        }

        public string HeaderBarCodeSeparatorDocumentManageError
        {
            get
            {
                return _HeaderBarCodeSeparatorDocumentManageError;
            }
            set
            {
                _HeaderBarCodeSeparatorDocumentManageError = value;
                RaisePropertyChanged("HeaderBarCodeSeparatorDocumentManageError");
            }
        }

        public string HeaderBarCodeSeparatorDocumentInfLimit
        {
            get
            {
                return _HeaderBarCodeSeparatorDocumentInfLimit;
            }
            set
            {
                _HeaderBarCodeSeparatorDocumentInfLimit = value;
                RaisePropertyChanged("HeaderBarCodeSeparatorDocumentInfLimit");
            }
        }

        public string HeaderBarCodeSeparatorDocumentSupLimit
        {
            get
            {
                return _HeaderBarCodeSeparatorDocumentSupLimit;
            }
            set
            {
                _HeaderBarCodeSeparatorDocumentSupLimit = value;
                RaisePropertyChanged("HeaderBarCodeSeparatorDocumentSupLimit");
            }
        }

        public string Button_Delete_Barcode_Document_Separator_Parameter
        {
            get
            {
                return _Button_Delete_Barcode_Document_Separator_Parameter;
            }
            set
            {
                _Button_Delete_Barcode_Document_Separator_Parameter = value;
                RaisePropertyChanged("Button_Delete_Barcode_Document_Separator_Parameter");
            }
        }

        public string Button_Validate_Barcode_Document_Separator_Parameter
        {
            get
            {
                return _Button_Validate_Barcode_Document_Separator_Parameter;
            }
            set
            {
                _Button_Validate_Barcode_Document_Separator_Parameter = value;
                RaisePropertyChanged("Button_Validate_Barcode_Document_Separator_Parameter");
            }
        }

        public ObservableCollection<DisplayBarCodeItem> BarCodeItemList
        {
            get
            {
                return _BarCodeItemList;
            }
            set
            {
                _BarCodeItemList = value;
                RaisePropertyChanged("BarCodeItemList");
            }
        }

        public DisplayBarCodeItem SelectedDisplayBarCodeItem
        {
            get
            {
                return _SelectedDisplayBarCodeItem;
            }
            set
            {
                _SelectedDisplayBarCodeItem = value;
                RaisePropertyChanged("SelectedDisplayBarCodeItem");
            }
        }

        public string Button_Save_BarCode
        {
            get
            {
                return _Button_Save_BarCode;
            }
            set
            {
                _Button_Save_BarCode = value;
                RaisePropertyChanged("Button_Save_BarCode");
            }
        }

        public Boolean IsEnabledAddBarCodeButton
        {
            get
            {
                return _IsEnabledAddBarCodeButton;
            }
            set
            {
                _IsEnabledAddBarCodeButton = value;
                RaisePropertyChanged("IsEnabledAddBarCodeButton");
            }
        }

        public string Button_Update_BarCode
        {
            get
            {
                return _Button_Update_BarCode;
            }
            set
            {
                _Button_Update_BarCode = value;
                RaisePropertyChanged("Button_Update_BarCode");
            }
        }

        public Boolean IsEnabledUpdateBarCodeButton
        {
            get
            {
                return _IsEnabledUpdateBarCodeButton;
            }
            set
            {
                _IsEnabledUpdateBarCodeButton = value;
                RaisePropertyChanged("IsEnabledUpdateBarCodeButton");
            }
        }

        public string Button_Close_BarCode
        {
            get
            {
                return _Button_Close_BarCode;
            }
            set
            {
                _Button_Close_BarCode = value;
                RaisePropertyChanged("Button_Close_BarCode");
            }
        }

        public Boolean CanInsertRowGridSepDoc
        {
            get
            {
                return _CanInsertRowGridSepDoc;
            }
            set
            {
                _CanInsertRowGridSepDoc = value;
                RaisePropertyChanged("CanInsertRowGridSepDoc");
            }
        }

        #endregion Properties

        #region Constructor and Initialization

        public BarCodeAddTabViewModel()
            : base()
        {
            CanCloseTab = true;

            //Icon.
            TabIcon = new Uri("/Rio.Manager.Application;component/Assets/Images/BarCode_Onglet_Add.png", UriKind.Relative);
        }

        /// <summary>Inits the commands.</summary>
        public override void InitCommands()
        {
            //Command initialization.
            InitializationCommand = new RelayCommand(Initialization);
            AddRowCommand = new RelayCommand(AddBarCodeSeparatorDocumentParameterGridViewRow);
            UpdateBarCodeCommand = new RelayCommand(UpdateAnBarCode);
            SaveBarCodeCommand = new RelayCommand(SaveNewBarCode);
            CloseBarCodeCommand = new RelayCommand(CloseAddBarCode);
        }

        /// <summary>Initializations this instance.</summary>
        private void Initialization()
        {
            InitLabels();
            InitsProperties();
            InitsButton();
            InitsBarcodeFunction(false);
            FillBarCodeTypologyType();
            FillBarCodeFunction();
            if ((IsModifyMode) && (BarCodeInformationToUpdate.Count != 0))
            {
                //Set properties for update part.
                SetBarCodeProperties();
            }

            //To initialize organization and folder type when necessary.
            if (IsModifyMode)
            {
                IsModifyMode = false;
            }
        }

        #region Add

        /// <summary>Inits the labels.</summary>
        public override void InitLabels()
        {
            //Retrieves the labels.
            //Label Initialization.
            if (IsModifyMode == false)
            {
                //Adding mode.
                TabTitle = Resx.GetLabel(ResxRioManager.BARCODE_ADD);
                TabIcon = new Uri("/Rio.Manager.Application;component/Assets/Images/BarCode_Onglet_Add.png", UriKind.Relative);
                BarCodeTitleAdd = Resx.GetLabel(ResxRioManager.BARCODE_TITLE_ADD);
            }
            else
            {
                //Modify mode.
                TabTitle = Resx.GetLabel(ResxRioManager.BARCODE_UPDATE);
                TabIcon = new Uri("/Rio.Manager.Application;component/Assets/Images/BarCode_Onglet_Manage.png", UriKind.Relative);
                BarCodeTitleAdd = Resx.GetLabel(ResxRioManager.BARCODE_TITLE_UPDATE);
            }
            BarCodeGeneralInformationAddTitle = Resx.GetLabel(ResxRioManager.BARCODE_GENERAL_INFORMATION_ADD);
            BarCodeIdentifierAddTitle = Resx.GetLabel(ResxRioManager.BARCODE_IDENTIFIER_ADD);
            BarCodeNameAddTitle = Resx.GetLabel(ResxRioManager.BARCODE_NAME_ADD);
            BarCodeDescriptionAddTitle = Resx.GetLabel(ResxRioManager.BARCODE_DESCRIPTION_ADD);
            BarCodeTypologyAddTitle = Resx.GetLabel(ResxRioManager.BARCODE_TYPOLOGY_INFORMATION_ADD);
            BarCodeTypologyPrefixAddTitle = Resx.GetLabel(ResxRioManager.BARCODE_TYPOLOGY_PREFIX_ADD);
            BarCodeTypologyTypeAddTitle = Resx.GetLabel(ResxRioManager.BARCODE_TYPOLOGY_TYPE_ADD);
            BarCodeFunctionAddTitle = Resx.GetLabel(ResxRioManager.BARCODE_FUNCTION_INFORMATION_ADD);
            ChbActivateBarCodeLabel = Resx.GetLabel(ResxRioManager.BARCODE_CHB_BARCODE_ACTIVATE);
            ChbModifyFunctionBarCodeLabel = Resx.GetLabel(ResxRioManager.BARCODE_CHB_FUNCTION_CHANGE_ENABLE);
            ChbFolderCorrespondenceLabel = Resx.GetLabel(ResxRioManager.BARCODE_CHB_FOLDER_CORRESPONDENCE_LABEL);
            ChbDeleteBarCodePageLabel = Resx.GetLabel(ResxRioManager.BARCODE_CHB_DELETE_BARCODE_PAGE_LABEL);
            ChbManageErrorLabel = Resx.GetLabel(ResxRioManager.BARCODE_CHB_MANAGE_ERROR_LABEL);
            ChbLimitLabel = Resx.GetLabel(ResxRioManager.BARCODE_CHB_LIMIT_LABEL);
            OrganizationLabel = Resx.GetLabel(ResxCommon.ORGANIZATION);
            FolderTypeLabel = Resx.GetLabel(ResxCommon.FOLDER_TYPE);
            BarCodeSingleIndexAddTitle = Resx.GetLabel(ResxRioManager.BARCODE_SINGLE_INDEX_ADD);
            BarcodeStartCharacterSingleIndexAddTitle = Resx.GetLabel(ResxRioManager.BARCODE_SINGLE_INDEX_START_CHARACTER_ADD);
            BarcodeEndCharacterSingleIndexAddTitle = Resx.GetLabel(ResxRioManager.BARCODE_SINGLE_INDEX_END_CHARACTER_ADD);
            BtnRadDocumentLimitLabel = Resx.GetLabel(ResxRioManager.BARCODE_CHB_DOCUMENT_LIMIT_LABEL);
            BtnRadPageLimitLabel = Resx.GetLabel(ResxRioManager.BARCODE_CHB_PAGE_LIMIT_LABEL);
            BarcodeLowerBoundLimitAddTitle = Resx.GetLabel(ResxRioManager.BARCODE_LOWER_BOUND_LIMIT_ADD);
            BarcodeUpeprBoundLimitAddTitle = Resx.GetLabel(ResxRioManager.BARCODE_UPPER_BOUND_LIMIT_ADD);
            HeaderBarCodeSeparatorDocumentId = Resx.GetLabel(ResxRioManager.BARCODE_SEP_DOC_ID);
            HeaderBarCodeSeparatorDocumentChooseNatureName = Resx.GetLabel(ResxRioManager.BARCODE_SEP_DOC_CHOOSE_ITEM_NAME);
            HeaderBarCodeSeparatorDocumentNatureName = Resx.GetLabel(ResxRioManager.BARCODE_SEP_DOC_NATURE_NAME);
            HeaderBarCodeSeparatorDocumentItemName = Resx.GetLabel(ResxRioManager.BARCODE_SEP_DOC_ITEM_NAME);
            HeaderBarCodeSeparatorDocumentPdfMerge = Resx.GetLabel(ResxRioManager.BARCODE_SEP_DOC_PDF_MERGE);
            HeaderBarCodeSeparatorDocumentDeleteBarCodePage = Resx.GetLabel(ResxRioManager.BARCODE_SEP_DOC_DELETE_BARCODE_PAGE);
            HeaderBarCodeSeparatorDocumentManageError = Resx.GetLabel(ResxRioManager.BARCODE_SEP_DOC_MANAGMENT_ERROR);
            HeaderBarCodeSeparatorDocumentInfLimit = Resx.GetLabel(ResxRioManager.BARCODE_SEP_DOC_INF_LIMIT);
            HeaderBarCodeSeparatorDocumentSupLimit = Resx.GetLabel(ResxRioManager.BARCODE_SEP_DOC_SUP_LIMIT);
            Button_Delete_Barcode_Document_Separator_Parameter = Resx.GetLabel(ResxCommon.DELETE);
            Button_Validate_Barcode_Document_Separator_Parameter = Resx.GetLabel(ResxCommon.VALIDATE);
            Button_Save_BarCode = Resx.GetLabel(ResxRioManager.BARCODE_BUTTON_SAVE);
            Button_Update_BarCode = Resx.GetLabel(ResxRioManager.BARCODE_BUTTON_UPDATE);
            Button_Close_BarCode = Resx.GetLabel(ResxRioManager.BARCODE_BUTTON_CLOSE);
        }

        /// <summary>Initialize all properties</summary>
        public void InitsProperties()
        {
            //Initialize generals properties.
            InitsGeneralsProperties();

            //Initialize folder separator partie. 
            InitsFolderSeparatorPartProperties();

            //Initialize  document separator partie.
            InitsDocumentSeparatorPartProperties();

            //Parameter function must be invisible.
            BarcodeFunctionFolderSeparatorVisible = false;
            BarcodeFunctionDocumentSeparatorVisible = false;
        }

        /// <summary>Initialize generals properties.</summary>
        public void InitsGeneralsProperties()
        {
            //Initialize generals properties.
            BarCodeIdentifierAdd = string.Empty;
            BarCodeNameAdd = string.Empty;
            BarCodeDescriptionAdd = string.Empty;
            ChbActiveBarCodeIsChecked = true;
            BarCodeTypologyPrefixAdd = string.Empty;

            //ComboBox barcode typology type.
            BarCodeTypologyTypeList = new ObservableCollection<DisplayBarCodeType>();
            SelectedDisplayBarCodeTypologyType = new DisplayBarCodeType();

            //ComboBox barcode function. 
            BarCodeFunctionNameList = new ObservableCollection<DisplayBarCodeFunction>();
            SelectedDisplayBarCodeFunction = new DisplayBarCodeFunction();
            if (!IsModifyMode)
            {
                IsEnabledcbFunctionName = true;
            }

            //RadGridView separator document parameters list.
            IsEditingRadGridViewBarcoDeSeparatorDocumentParametersAdd = false;
        }

        /// <summary>Initialize Folder separator partie.</summary>
        public void InitsFolderSeparatorPartProperties()
        {
            //Initialize Folder separator partie. 

            //CheckBox Is not checked.
            CheckBoxIsChecked(false);

            //FolderType and organization.
            if (!IsModifyMode)
            {
                NeedToRefreshOrganizationAndFolderType(BarcodeSeparatorType.FolderSeparator);
            }

            //ComboBox single index must be empty.
            BarCodeSingleIndexList = new ObservableCollection<DisplayBarCodeSingleIndex>();
            SelectedDisplayBarCodeSingleIndex = new DisplayBarCodeSingleIndex();

            //RadNumericUpDown controle
            BarcodeStartCharacterSingleIndex = 0;
            BarcodeEndCharacterSingleIndex = 0;
            BarcodeLowerBoundLimit = 0;
            BarcodeUpperBoundLimit = 0;

            //RadioButton must not to be checked.
            BtnRadDocumentLimitIsChecked = false;
            BtnRadPageLimitIsChecked = false;

            //Single indexes parameters aren't active.
            IsEnabledParameterSingleIndexGroupControl = false;
        }

        /// <summary>Initialize document separator partie.</summary>
        public void InitsDocumentSeparatorPartProperties()
        {
            //Initialize document separator partie. 

            //FolderType and organization.
            if (!IsModifyMode)
            {
                NeedToRefreshOrganizationAndFolderType(BarcodeSeparatorType.DocumentSeparator);
            }

            //ComboBox single item must be empty.
            BarCodeItemList = new ObservableCollection<DisplayBarCodeItem>();
            SelectedDisplayBarCodeItem = new DisplayBarCodeItem();

            //Grid parameters must to be empty.
            IsEditingRadGridViewBarcoDeSeparatorDocumentParametersAdd = false;
            BarCodeSeparatorDocumentParametersList = new ObservableCollection<BarcodeDocumentSeparator>();
        }

        /// <summary>Initialize folder correspondence part.</summary>
        public void InitsFolderCorrespondencePartProperties()
        {
            if (!IsModifyMode)
            {
                NeedToRefreshOrganizationAndFolderType(BarcodeSeparatorType.FolderSeparator);
            }

            BarCodeSingleIndexList = new ObservableCollection<DisplayBarCodeSingleIndex>();
            SelectedDisplayBarCodeSingleIndex = new DisplayBarCodeSingleIndex();
            IsEnabledParameterSingleIndexGroupControl = false;
            BarcodeStartCharacterSingleIndex = 0;
            BarcodeEndCharacterSingleIndex = 0;
        }

        /// <summary>Initialize limit folder part.</summary>
        public void InitsLimitFolderPartProperties()
        {
            //RadNumericUpDown controle
            BarcodeLowerBoundLimit = 0;
            BarcodeUpperBoundLimit = 0;

            //RadioButton must not to be checked.
            BtnRadDocumentLimitIsChecked = false;
            BtnRadPageLimitIsChecked = false;
        }

        /// <summary>Show modify or create BarCode button.</summary>
        public void InitsButton()
        {
            //Button isn't enabled.  
            IsEnabledUpdateBarCodeButton = false;
            IsEnabledAddBarCodeButton = false;

            //Show add or modify barcode button.
            if (IsModifyMode == false)
            {
                //Adding Mode.
                IsEnabledAddBarCodeButton = true;
            }
            else
            {
                //Modify Mode.
                IsEnabledUpdateBarCodeButton = true;
            }
        }

        /// <summary>Show or not show barcode function.</summary>
        /// <param name="p_bStatus">Status function view</param>
        public void InitsBarcodeFunction(Boolean p_bStatus)
        {
            //Status barcode function function updated.
            BarcodeFunctionDocumentSeparatorVisible = p_bStatus;
            BarcodeFunctionFolderSeparatorVisible = p_bStatus;
        }

        #endregion Add

        #region Update

        /// <summary>
        /// Set barcode properties
        /// </summary>
        public void SetBarCodeProperties()
        {
            //Initialize generals properties.
            SetGeneralsProperties();

            if (BarCodeInformationToUpdate[0].Function == BarcodeSeparatorType.FolderSeparator)
            {
                //Initialize folder separator partie. 
                SetFolderSeparatorPartProperties();
            }
            else
            {
                //Initialize  document separator partie.
                SetDocumentSeparatorPartProperties();
            }
        }

        /// <summary>
        /// Set generals properties.
        /// </summary>
        public void SetGeneralsProperties()
        {
            //Set generals properties.
            BarCodeIdentifierAdd = BarCodeInformationToUpdate[0].ParamCode;
            BarCodeNameAdd = BarCodeInformationToUpdate[0].Name;
            BarCodeDescriptionAdd = BarCodeInformationToUpdate[0].Description;
            ChbActiveBarCodeIsChecked = BarCodeInformationToUpdate[0].Enabled;
            BarCodeTypologyPrefixAdd = BarCodeInformationToUpdate[0].Prefix;

            //ComboBox barcode typology type.
            foreach (DisplayBarCodeType l_oBarCodeType in BarCodeTypologyTypeList)
            {
                if (l_oBarCodeType.Value == BarCodeInformationToUpdate[0].Type)
                {
                    SelectedDisplayBarCodeTypologyType = l_oBarCodeType;
                    break;
                }
            }

            //ComboBox barcode function.
            foreach (DisplayBarCodeFunction l_oBarCodeFunction in BarCodeFunctionNameList)
            {
                if (l_oBarCodeFunction.Value == BarCodeInformationToUpdate[0].Function)
                {
                    SelectedDisplayBarCodeFunction = l_oBarCodeFunction;
                    IsEnabledcbFunctionName = false;
                    break;
                }
            }

            //RadGridVieux separator document parameters list.
            IsEditingRadGridViewBarcoDeSeparatorDocumentParametersAdd = false;
        }

        /// <summary>Set Folder separator partie.</summary>
        public void SetFolderSeparatorPartProperties()
        {
            //Set Folder separator partie. 
            List<BarcodeSeparator> l_oBarCodeSeparatorItem = BarCodeInformationToUpdate[0].BarCodeSeparatorList.ToList();

            //CheckBox Is not checked.
            ChbFolderCorrespondenceIsChecked = ((BarcodeFolderSeparator)l_oBarCodeSeparatorItem[0]).CorrespondenceFolder;
            ChbDeleteBarCodePageIsChecked = l_oBarCodeSeparatorItem[0].DeleteBarCodePage;
            ChbManageErrorIsChecked = l_oBarCodeSeparatorItem[0].ErrorManagment;
            ChbLimitIsChecked = l_oBarCodeSeparatorItem[0].DocLimit;

            //ComboBox single index must be empty.
            FillFolderTypeSingleIndexes();
            foreach (DisplayBarCodeSingleIndex l_oBarCodeSingleIndex in BarCodeSingleIndexList)
            {
                if (l_oBarCodeSingleIndex.Value == ((BarcodeFolderSeparator)l_oBarCodeSeparatorItem[0]).UniqueIndexIdentifier)
                {
                    SelectedDisplayBarCodeSingleIndex = l_oBarCodeSingleIndex;
                    break;
                }
            }

            //RadNumericUpDown controle
            BarcodeStartCharacterSingleIndex = ((BarcodeFolderSeparator)l_oBarCodeSeparatorItem[0]).StartChar;
            BarcodeEndCharacterSingleIndex = ((BarcodeFolderSeparator)l_oBarCodeSeparatorItem[0]).EndChar;
            BarcodeLowerBoundLimit = l_oBarCodeSeparatorItem[0].InfLimit;
            BarcodeUpperBoundLimit = l_oBarCodeSeparatorItem[0].SupLimit;

            //RadioButton must not to be checked.
            BtnRadDocumentLimitIsChecked = ((BarcodeFolderSeparator)l_oBarCodeSeparatorItem[0]).DocumentLimit;
            BtnRadPageLimitIsChecked = ((BarcodeFolderSeparator)l_oBarCodeSeparatorItem[0]).PageLimit;

            //Single indexes parameters aren't active.
            IsEnabledParameterSingleIndexGroupControl = false;
        }

        /// <summary>Set Document separator partie.</summary>
        public void SetDocumentSeparatorPartProperties()
        {
            //Retrieves document separator barcode.
            InitEmptyBarCodeSeparatorDocumentParameterGrid();
        }

        /// <summary>Inits the barcode separator document grid to add parameters.</summary>
        public void InitEmptyBarCodeSeparatorDocumentParameterGrid()
        {
            //Retrieves Folder type items list.
            FillBarCodeFolderTypeItem();

            //Built barcode document type parameters.
            //ObservableCollection<BarcodeSeparator> l_oBarCodeSeparatorDocumentsList = new ObservableCollection<BarcodeSeparator>();
            foreach (BarcodeSeparator l_oBarCodeSeparatorDocumentItem in BarCodeInformationToUpdate[0].BarCodeSeparatorList.ToList())  //BarCodeSeparatorDocumentParametersList 
            {
                BarcodeDocumentSeparator l_oBarCodeSeparatorDocumentItemTemp = new BarcodeDocumentSeparator();
                l_oBarCodeSeparatorDocumentItemTemp.Identifier = l_oBarCodeSeparatorDocumentItem.Identifier;
                l_oBarCodeSeparatorDocumentItemTemp.CabIdentifier = l_oBarCodeSeparatorDocumentItem.CabIdentifier;
                l_oBarCodeSeparatorDocumentItemTemp.ParamCode = ((BarcodeDocumentSeparator)l_oBarCodeSeparatorDocumentItem).ParamCode;
                l_oBarCodeSeparatorDocumentItemTemp.OrganizationId = l_oBarCodeSeparatorDocumentItem.OrganizationId;
                l_oBarCodeSeparatorDocumentItemTemp.FolderTypeId = l_oBarCodeSeparatorDocumentItem.FolderTypeId;
                l_oBarCodeSeparatorDocumentItemTemp.NatureIdentifier = ((BarcodeDocumentSeparator)l_oBarCodeSeparatorDocumentItem).NatureIdentifier;
                l_oBarCodeSeparatorDocumentItemTemp.NatureName = ((BarcodeDocumentSeparator)l_oBarCodeSeparatorDocumentItem).NatureName;
                l_oBarCodeSeparatorDocumentItemTemp.ItemIdentifier = ((BarcodeDocumentSeparator)l_oBarCodeSeparatorDocumentItem).ItemIdentifier;
                l_oBarCodeSeparatorDocumentItemTemp.ItemCode = ((BarcodeDocumentSeparator)l_oBarCodeSeparatorDocumentItem).ItemCode;
                l_oBarCodeSeparatorDocumentItemTemp.ItemName = ((BarcodeDocumentSeparator)l_oBarCodeSeparatorDocumentItem).ItemName;
                l_oBarCodeSeparatorDocumentItemTemp.PdfMerge = ((BarcodeDocumentSeparator)l_oBarCodeSeparatorDocumentItem).PdfMerge;
                l_oBarCodeSeparatorDocumentItemTemp.DeleteBarCodePage = l_oBarCodeSeparatorDocumentItem.DeleteBarCodePage;
                l_oBarCodeSeparatorDocumentItemTemp.ErrorManagment = l_oBarCodeSeparatorDocumentItem.ErrorManagment;
                l_oBarCodeSeparatorDocumentItemTemp.DocLimit = l_oBarCodeSeparatorDocumentItem.DocLimit;
                l_oBarCodeSeparatorDocumentItemTemp.InfLimit = l_oBarCodeSeparatorDocumentItem.InfLimit;
                l_oBarCodeSeparatorDocumentItemTemp.SupLimit = l_oBarCodeSeparatorDocumentItem.SupLimit;
                BarCodeSeparatorDocumentParametersList.Add(l_oBarCodeSeparatorDocumentItemTemp);
            }
        }

        #endregion Update

        #endregion Constructor and Initialization

        #region Methods

        /// <summary>
        /// Needs to refresh consultation barcode saved.
        /// </summary>
        private void NeedToRefreshBarCodeConsultationList()
        {
            //Message send to barcode consultation actualisation. --> BarCodeTabViewModel.cs
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage("RefreshConsultBarcode"));
        }

        /// <summary>
        /// Needs to refresh organization and folder type control.
        /// </summary>
        private void NeedToRefreshOrganizationAndFolderType(BarcodeSeparatorType p_oBarcodeSeparatorType)
        {
            //Message send to relaod oragnization and folder type control. --> BarCodeAddTabView.xaml.cs
            switch (p_oBarcodeSeparatorType)
            {
                case BarcodeSeparatorType.None:
                    break;
                case BarcodeSeparatorType.FolderSeparator:
                    Messenger.Default.Send<NotificationMessage>(new NotificationMessage(BarcodeSeparatorType.FolderSeparator.ToString()));
                    break;
                case BarcodeSeparatorType.DocumentSeparator:
                    Messenger.Default.Send<NotificationMessage>(new NotificationMessage(BarcodeSeparatorType.DocumentSeparator.ToString()));
                    break;
                default:
                    throw new ArgumentOutOfRangeException("BarCodeAddTabViewModel.NeedToRefreshOrganizationAndFolderType: This barcode separator type: " + p_oBarcodeSeparatorType.ToString() + " isn't supported");
            }
        }

        /// <summary>
        /// Fills the folder type single indexes.
        /// </summary>
        public void FillFolderTypeSingleIndexes()
        {
            //Variable initialization.
            Boolean l_oAddButtonIsEnablePropertie = IsEnabledAddBarCodeButton;
            Boolean l_oUpdateButtonIsEnablePropertie = IsEnabledUpdateBarCodeButton;
            List<Index> l_oSingleIndexList = new List<Index>();

            //Retrieves singles indexes.
            if (SelectedFolderType != null)
            {
                l_oSingleIndexList = SelectedFolderType.LstIndex.Where(f => f.IsUnique).ToList();
            }


            //FolderType selected contains single unique or not?
            if (l_oSingleIndexList.Count == 0)
            {
                if (SelectedFolderType != null)
                {
                    MessageBoxChildWindow L_oMessage = new MessageBoxChildWindow(String.Empty, string.Format(Resx.GetLabel(ResxRioManager.BARCODE_MSG_NOT_SINGLE_INDEX), SelectedFolderType.Label).Replace("\\n", Environment.NewLine), MessageBoxButtons.Ok, MessageBoxIcon.Warning);
                    L_oMessage.Show();
                    L_oMessage.Closed += (s, ev) =>
                    {
                        //Reinitialize all.
                        IsEnabledAddBarCodeButton = l_oAddButtonIsEnablePropertie;
                        IsEnabledUpdateBarCodeButton = l_oUpdateButtonIsEnablePropertie;
                        SelectedFolderType = null;
                        BarCodeSingleIndexList = new ObservableCollection<DisplayBarCodeSingleIndex>();
                        SelectedDisplayBarCodeSingleIndex = new DisplayBarCodeSingleIndex();
                        IsEnabledParameterSingleIndexGroupControl = false;
                    };
                    return;
                }
            }

            //Single indexes parameters are active.
            IsEnabledParameterSingleIndexGroupControl = true;

            //Fill all the barcode function.
            if (BarCodeSingleIndexList.Count > 0)
            {
                //New list.
                BarCodeSingleIndexList = new ObservableCollection<DisplayBarCodeSingleIndex>();
                SelectedDisplayBarCodeSingleIndex = null;
            }
            foreach (Index l_oSingleIndex in l_oSingleIndexList)
            {
                BarCodeSingleIndexList.Add(new DisplayBarCodeSingleIndex(l_oSingleIndex.IndexLabel, l_oSingleIndex.Indentifier));
            }
        }

        /// <summary>
        /// CheckBox Status
        /// </summary>
        /// <param name="p_bStatus">Status checkbox</param>
        public void CheckBoxIsChecked(Boolean p_bStatus)
        {
            //Status CheckBox updated.
            ChbFolderCorrespondenceIsChecked = p_bStatus;
            ChbDeleteBarCodePageIsChecked = p_bStatus;
            ChbManageErrorIsChecked = p_bStatus;
            ChbLimitIsChecked = p_bStatus;
        }

        /// <summary>
        /// Fill barcode combobox function.
        /// </summary>
        public void FillBarCodeFunction()
        {
            //Fill all the barcode function.
            if (BarCodeFunctionNameList.Count == 0)
            {
                BarCodeFunctionNameList.Add(new DisplayBarCodeFunction(Resx.GetLabel(ResxRioManager.BARCODE_FUNCTION_FOLDER_SEPARATOR), BarcodeSeparatorType.FolderSeparator));
                BarCodeFunctionNameList.Add(new DisplayBarCodeFunction(Resx.GetLabel(ResxRioManager.BARCODE_FUNCTION_DOCUMENT_SEPARATOR), BarcodeSeparatorType.DocumentSeparator));
            }
        }

        /// <summary>
        /// Fill barcode combobox typology type.
        /// </summary>
        public void FillBarCodeTypologyType()
        {
            //Fill all the barcode function.
            if (BarCodeTypologyTypeList.Count == 0)
            {
                BarCodeTypologyTypeList.Add(new DisplayBarCodeType(BarcodeType.Barcode1DReaderADD2.szGetDescription(), BarcodeType.Barcode1DReaderADD2));
                BarCodeTypologyTypeList.Add(new DisplayBarCodeType(BarcodeType.Barcode1DReaderADD5.szGetDescription(), BarcodeType.Barcode1DReaderADD5));
                BarCodeTypologyTypeList.Add(new DisplayBarCodeType(BarcodeType.Barcode1DReaderBcdMatrix.szGetDescription(), BarcodeType.Barcode1DReaderBcdMatrix));
                BarCodeTypologyTypeList.Add(new DisplayBarCodeType(BarcodeType.Barcode1DReaderCode128.szGetDescription(), BarcodeType.Barcode1DReaderCode128));
                BarCodeTypologyTypeList.Add(new DisplayBarCodeType(BarcodeType.Barcode1DReaderCode39.szGetDescription(), BarcodeType.Barcode1DReaderCode39));
                BarCodeTypologyTypeList.Add(new DisplayBarCodeType(BarcodeType.Barcode1DReaderCODE93.szGetDescription(), BarcodeType.Barcode1DReaderCODE93));
                BarCodeTypologyTypeList.Add(new DisplayBarCodeType(BarcodeType.Barcode1DReaderCodeabar.szGetDescription(), BarcodeType.Barcode1DReaderCodeabar));
                BarCodeTypologyTypeList.Add(new DisplayBarCodeType(BarcodeType.Barcode1DReaderDataLogic2of5.szGetDescription(), BarcodeType.Barcode1DReaderDataLogic2of5));
                BarCodeTypologyTypeList.Add(new DisplayBarCodeType(BarcodeType.Barcode1DReaderEAN128.szGetDescription(), BarcodeType.Barcode1DReaderEAN128));
                BarCodeTypologyTypeList.Add(new DisplayBarCodeType(BarcodeType.Barcode1DReaderEAN13.szGetDescription(), BarcodeType.Barcode1DReaderEAN13));
                BarCodeTypologyTypeList.Add(new DisplayBarCodeType(BarcodeType.Barcode1DReaderEAN8.szGetDescription(), BarcodeType.Barcode1DReaderEAN8));
                BarCodeTypologyTypeList.Add(new DisplayBarCodeType(BarcodeType.Barcode1DReaderIata2of5.szGetDescription(), BarcodeType.Barcode1DReaderIata2of5));
                BarCodeTypologyTypeList.Add(new DisplayBarCodeType(BarcodeType.Barcode1DReaderIndustrial2of5.szGetDescription(), BarcodeType.Barcode1DReaderIndustrial2of5));
                BarCodeTypologyTypeList.Add(new DisplayBarCodeType(BarcodeType.Barcode1DReaderInterleaved2of5.szGetDescription(), BarcodeType.Barcode1DReaderInterleaved2of5));
                BarCodeTypologyTypeList.Add(new DisplayBarCodeType(BarcodeType.Barcode1DReaderInverted2of5.szGetDescription(), BarcodeType.Barcode1DReaderInverted2of5));
                BarCodeTypologyTypeList.Add(new DisplayBarCodeType(BarcodeType.Barcode1DReaderMatrix2of5.szGetDescription(), BarcodeType.Barcode1DReaderMatrix2of5));
                BarCodeTypologyTypeList.Add(new DisplayBarCodeType(BarcodeType.Barcode1DReaderUPCA.szGetDescription(), BarcodeType.Barcode1DReaderUPCA));
                BarCodeTypologyTypeList.Add(new DisplayBarCodeType(BarcodeType.Barcode1DReaderUPCE.szGetDescription(), BarcodeType.Barcode1DReaderUPCE));
            }
        }

        /// <summary>Fill barcode combobox folderType item if user want case with message.</summary>
        //public void FillOrNotFillBarCodeFolderTypeItem()
        //{            
        //    try
        //    {
        //        //FolderType selected contains single unique or not?
        //        if (BarCodeSeparatorDocumentParametersList.Count != 0)
        //        {
        //            MessageBoxChildWindow L_oMessage = new MessageBoxChildWindow(String.Empty, string.Format(Resx.GetLabel(ResxRioManager.BARCODE_MSG_FILL_OTHER_ITEM), SelectedFolderType.Label).Replace("\\n", Environment.NewLine), MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
        //            L_oMessage.ShowDialog();
        //            L_oMessage.Closed += (s, ev) =>
        //            {
        //                //Retrieves item folder type if necessary.
        //                if (L_oMessage.Result == MessageBoxResult.Yes)
        //                {                          
        //                    FillBarCodeFolderTypeItem();
        //                }                      
        //            };
        //            return;
        //        }            
        //    }
        //    catch (Exception)
        //    {
        //        //Retrieves item folder type.
        //        FillBarCodeFolderTypeItem();                
        //    }
        //}        

        /// <summary>
        /// Fill barcode combobox folderType item.
        /// </summary>
        public void FillBarCodeFolderTypeItem()
        {
            //Retrieves folder type item.
            List<Item> l_oItemList = SelectedFolderType.LstItems.ToList();

            //FolderType selected contains items or not?
            if (l_oItemList.Count > 0)
            {
                if (!IsModifyMode)
                {
                    //New List of parameters.
                    BarCodeSeparatorDocumentParametersList = new ObservableCollection<BarcodeDocumentSeparator>();
                }

                //Fill all the combobox folderType item.
                if (BarCodeItemList.Count > 0)
                {
                    //New list.
                    BarCodeItemList = new ObservableCollection<DisplayBarCodeItem>();
                    SelectedDisplayBarCodeItem = new DisplayBarCodeItem();
                }
                foreach (Item l_oItem in l_oItemList)
                {
                    BarCodeItemList.Add(new DisplayBarCodeItem(l_oItem.Name, l_oItem.Identifier));
                }

                //User can insert an row in the grid.
                CanInsertRowGridSepDoc = true;
            }
            else
            {
                MessageBoxChildWindow L_oMessage = new MessageBoxChildWindow(String.Empty, string.Format(Resx.GetLabel(ResxRioManager.BARCODE_MSG_NOT_ITEMS), SelectedFolderType.Label).Replace("\\n", Environment.NewLine), MessageBoxButtons.Ok, MessageBoxIcon.Warning);
                L_oMessage.Show();
                L_oMessage.Closed += (s, ev) =>
                {
                    //Reinitialize.
                    SelectedFolderType = null;
                    //User can't insert an row in the grid.
                    CanInsertRowGridSepDoc = false; 
                }; return;
            }
        }

        /// <summary>
        /// Fill parameters functions barcode selected.
        /// </summary>
        public void BarCodeFillParametersFunctionSelected()
        {
            if (_SelectedDisplayBarCodeFunction != null)
            {
                //Show selected barcode function details.
                switch (_SelectedDisplayBarCodeFunction.Value)
                {
                    case BarcodeSeparatorType.DocumentSeparator:
                        //Document separator is selected.                    
                        BarcodeFunctionFolderSeparatorVisible = false;
                        BarcodeFunctionDocumentSeparatorVisible = true;
                        InitsDocumentSeparatorPartProperties();
                        break;

                    case BarcodeSeparatorType.FolderSeparator:
                        //Folder separator is selected.
                        BarcodeFunctionFolderSeparatorVisible = true;
                        BarcodeFunctionDocumentSeparatorVisible = false;
                        InitsFolderSeparatorPartProperties();
                        break;
                }

                //Description.
                BarCodeSelectedFunctionAddTitle = _SelectedDisplayBarCodeFunction.Name;
            }
        }

        /// <summary>
        /// Adds the barcode separator document parameter command.
        /// </summary>
        public void AddBarCodeSeparatorDocumentParameterGridViewRow()
        {
            //Insert barcode Parameters gridview rows.
            if (BarCodeSeparatorDocumentParametersList == null)
            {
                BarCodeSeparatorDocumentParametersList = new ObservableCollection<BarcodeDocumentSeparator>();
            }

            //Grid is in editing mode.
            IsEditingRadGridViewBarcoDeSeparatorDocumentParametersAdd = true;
        }

        /// <summary>
        /// Test to do before update or add an barcode in the database.
        /// </summary>
        /// <returns></returns>
        private Boolean TestBeforeAddOrUpdateBarCode()
        {
            //Variable initialization.
            Boolean l_bResult = true;
            Boolean l_oAddButtonIsEnablePropertie = IsEnabledAddBarCodeButton;
            Boolean l_oUpdateButtonIsEnablePropertie = IsEnabledUpdateBarCodeButton;
            string l_sMessage = string.Empty;
            List<string> l_oListEmptyElement = new List<string>();

            #region TestGeneralsBarCodeElements

            //Barcode identifiant, name and type and function can't to be null.
            if (BarCodeIdentifierAdd == string.Empty)
            {
                l_oListEmptyElement.Add(BarCodeIdentifierAddTitle.ToLower());
            }
            if (BarCodeNameAdd == string.Empty)
            {
                l_oListEmptyElement.Add(BarCodeNameAddTitle.ToLower());
            }
            if (SelectedDisplayBarCodeTypologyType.Name == string.Empty)
            {
                l_oListEmptyElement.Add(BarCodeTypologyTypeAddTitle.ToLower());
            }
            if (SelectedDisplayBarCodeFunction.Name == string.Empty)
            {
                l_oListEmptyElement.Add(BarCodeFunctionAddTitle.ToLower());
            }
            if (BarCodeTypologyPrefixAdd == string.Empty)
            {
                l_oListEmptyElement.Add(BarCodeTypologyPrefixAddTitle.ToLower());
            }

            //Test.
            if (l_oListEmptyElement.Count != 0)
            {
                //Return value.
                l_bResult = false;

                //User message.
                if (l_oListEmptyElement.Count == 1)
                {
                    MessageBoxChildWindow L_oMessage = new MessageBoxChildWindow(String.Empty, string.Format(Resx.GetLabel(ResxRioManager.BARCODE_MSG_MANDATORY_ELEMENT), l_oListEmptyElement[0]), MessageBoxButtons.Ok, MessageBoxIcon.Error);
                    L_oMessage.Show();
                    L_oMessage.Closed += (s, ev) =>
                    {
                        l_bResult = false;
                        IsEnabledAddBarCodeButton = l_oAddButtonIsEnablePropertie;
                        IsEnabledUpdateBarCodeButton = l_oUpdateButtonIsEnablePropertie;
                    };
                }
                else
                {
                    //Build message.
                    l_sMessage = string.Format("{0}'", l_sMessage);
                    l_sMessage = l_oListEmptyElement.Aggregate(l_sMessage, (current, s) => current + string.Format("{0}', '", s));

                    //Format message.
                    l_sMessage = l_sMessage.Substring(0, l_sMessage.Length - 3);
                    l_sMessage = string.Format("{0} {1} {2}", l_sMessage.Substring(0, l_sMessage.LastIndexOf(",")), Resx.GetLabel(ResxCommon.AND), l_sMessage.Substring(l_sMessage.LastIndexOf(",") + 1, l_sMessage.Length - l_sMessage.LastIndexOf(",") - 1));

                    //User message.
                    MessageBoxChildWindow L_oMessage = new MessageBoxChildWindow(String.Empty, string.Format(Resx.GetLabel(ResxRioManager.BARCODE_MSG_MANDATORY_ELEMENTS), l_oListEmptyElement.Count, l_sMessage), MessageBoxButtons.Ok, MessageBoxIcon.Error);
                    L_oMessage.Show();
                    L_oMessage.Closed += (s, ev) =>
                    {
                        l_bResult = false;
                        IsEnabledAddBarCodeButton = l_oAddButtonIsEnablePropertie;
                        IsEnabledUpdateBarCodeButton = l_oUpdateButtonIsEnablePropertie;
                    };
                }
                //General test is bad.
                return l_bResult;
            }

            #endregion TestGeneralsBarCodeElements

            #region TestParametersFonction

            //At least one parameter of the function is completely.
            switch (this.SelectedDisplayBarCodeFunction.Value)
            {
                case BarcodeSeparatorType.FolderSeparator:

                    #region Option function test.

                    if ((!this.ChbFolderCorrespondenceIsChecked) && (!this.ChbDeleteBarCodePageIsChecked) && (!this.ChbManageErrorIsChecked) && (!this.ChbLimitIsChecked))
                    {
                        //Return value.
                        l_bResult = false;

                        //User message.
                        MessageBoxChildWindow L_oMessage = new MessageBoxChildWindow(String.Empty, string.Format(Resx.GetLabel(ResxRioManager.BARCODE_MSG_NOT_FOLDER_FUNCTION_OPTION), this.SelectedDisplayBarCodeFunction.Name.ToLower()), MessageBoxButtons.Ok, MessageBoxIcon.Error);
                        L_oMessage.Show();
                        L_oMessage.Closed += (s, ev) =>
                        {
                            l_bResult = false;
                            this.IsEnabledAddBarCodeButton = l_oAddButtonIsEnablePropertie;
                            this.IsEnabledUpdateBarCodeButton = l_oUpdateButtonIsEnablePropertie;
                        };

                        //Option function test is bad.
                        return l_bResult;
                    }

                    #endregion Option function test.

                    #region Option Correspondence folder test

                    //Test Single Index for folder correspondence option.
                    if ((this.ChbFolderCorrespondenceIsChecked) && (this.SelectedDisplayBarCodeSingleIndex.Name == string.Empty))
                    {
                        //Return value.
                        l_bResult = false;

                        MessageBoxChildWindow L_oMessage = new MessageBoxChildWindow(String.Empty, string.Format(Resx.GetLabel(ResxRioManager.BARCODE_MSG_NOT_SINGLE_INDEX_CORRESPONDENCE_FOLDER_OPTION), this.ChbFolderCorrespondenceLabel.ToLower(), this.SelectedDisplayBarCodeFunction.Name.ToLower()), MessageBoxButtons.Ok, MessageBoxIcon.Error);
                        L_oMessage.Show();
                        L_oMessage.Closed += (s, ev) =>
                        {
                            l_bResult = false;
                            this.IsEnabledAddBarCodeButton = l_oAddButtonIsEnablePropertie;
                            this.IsEnabledUpdateBarCodeButton = l_oUpdateButtonIsEnablePropertie;
                        };

                        //Single index test is bad.
                        return l_bResult;
                    }

                    #endregion Option Correspondence folder test

                    #region Option Document Limit (checkbox)

                    //Document limit test (checkbox).
                    if ((!this.BtnRadDocumentLimitIsChecked) && (!this.BtnRadPageLimitIsChecked) && (this.ChbLimitIsChecked))
                    {
                        //Return value.
                        l_bResult = false;

                        MessageBoxChildWindow L_oMessage = new MessageBoxChildWindow(String.Empty, Resx.GetLabel(ResxRioManager.BARCODE_MSG_NOT_DOC_LIMIT_NATURE), MessageBoxButtons.Ok, MessageBoxIcon.Error);
                        L_oMessage.Show();
                        L_oMessage.Closed += (s, ev) =>
                        {
                            l_bResult = false;
                            this.IsEnabledAddBarCodeButton = l_oAddButtonIsEnablePropertie;
                            this.IsEnabledUpdateBarCodeButton = l_oUpdateButtonIsEnablePropertie;
                        };

                        //Document limit test checkbox is bad.
                        return l_bResult;
                    }

                    #endregion Option Document Limit (checkbox)

                    #region Option document limit bound

                    //Document limit bound test.
                    if ((this.BarcodeLowerBoundLimit == 0) && (this.BarcodeUpperBoundLimit == 0) && (this.ChbLimitIsChecked))
                    {
                        //Return value.
                        l_bResult = false;

                        MessageBoxChildWindow L_oMessage = new MessageBoxChildWindow(String.Empty, Resx.GetLabel(ResxRioManager.BARCODE_MSG_NOT_DOC_LIMIT_BOUND), MessageBoxButtons.Ok, MessageBoxIcon.Error);
                        L_oMessage.Show();
                        L_oMessage.Closed += (s, ev) =>
                        {
                            l_bResult = false;
                            this.IsEnabledAddBarCodeButton = l_oAddButtonIsEnablePropertie;
                            this.IsEnabledUpdateBarCodeButton = l_oUpdateButtonIsEnablePropertie;
                        };

                        //Document limit bound test is bad.
                        return l_bResult;
                    }

                    #endregion Option document limit bound

                    break;
                case BarcodeSeparatorType.DocumentSeparator:

                    #region Folder type Separator document test

                    //Folder type test.
                    if (this.SelectedFolderType.Label == null)
                    {
                        //Return value.
                        l_bResult = false;

                        MessageBoxChildWindow L_oMessage = new MessageBoxChildWindow(String.Empty, string.Format(Resx.GetLabel(ResxRioManager.BARCODE_MSG_NOT_FOLDER_TYPE_DOC), this.SelectedDisplayBarCodeFunction.Name.ToLower()), MessageBoxButtons.Ok, MessageBoxIcon.Error);
                        L_oMessage.Show();
                        L_oMessage.Closed += (s, ev) =>
                        {
                            l_bResult = false;
                            this.IsEnabledAddBarCodeButton = l_oAddButtonIsEnablePropertie;
                            this.IsEnabledUpdateBarCodeButton = l_oUpdateButtonIsEnablePropertie;
                        };

                        //Folder type test is bad.
                        return l_bResult;
                    }

                    #endregion Folder type Separator document test

                    #region Parameters List separator document mode test (IsEditing).

                    //Parameters List separator document mode test.
                    if ((this.IsEditingRadGridViewBarcoDeSeparatorDocumentParametersAdd == true) && (this.SelectedDisplayBarCodeFunction.Value == BarcodeSeparatorType.DocumentSeparator))
                    {
                        //Return value.
                        l_bResult = false;

                        MessageBoxChildWindow L_oMessage = new MessageBoxChildWindow(String.Empty, string.Format(Resx.GetLabel(ResxRioManager.BARCODE_MSG_FOLDER_TYPE_DOC_PARAMETER_ISEDITING), this.SelectedDisplayBarCodeFunction.Name.ToLower(), this.SelectedFolderType.Label.ToLower()), MessageBoxButtons.Ok, MessageBoxIcon.Error);
                        L_oMessage.Show();
                        L_oMessage.Closed += (s, ev) =>
                        {
                            l_bResult = false;
                            this.IsEnabledAddBarCodeButton = l_oAddButtonIsEnablePropertie;
                            this.IsEnabledUpdateBarCodeButton = l_oUpdateButtonIsEnablePropertie;
                        };

                        //Parameters List separator document mode test is bad.
                        return l_bResult;
                    }

                    #endregion Parameters List separator document mode test (IsEditing).

                    #region Parameters separator document test.

                    //Parameters separator document test..
                    if ((this.BarCodeSeparatorDocumentParametersList.Count == 0) && (this.SelectedDisplayBarCodeFunction.Value == BarcodeSeparatorType.DocumentSeparator))
                    {
                        //Return value.
                        l_bResult = false;

                        MessageBoxChildWindow L_oMessage = new MessageBoxChildWindow(String.Empty, string.Format(Resx.GetLabel(ResxRioManager.BARCODE_MSG_NOT_FOLDER_TYPE_DOC_PARAMETER), this.SelectedDisplayBarCodeFunction.Name.ToLower(), this.SelectedFolderType.Label.ToLower()), MessageBoxButtons.Ok, MessageBoxIcon.Error);
                        L_oMessage.Show();
                        L_oMessage.Closed += (s, ev) =>
                        {
                            l_bResult = false;
                            this.IsEnabledAddBarCodeButton = l_oAddButtonIsEnablePropertie;
                            this.IsEnabledUpdateBarCodeButton = l_oUpdateButtonIsEnablePropertie;
                        };

                        //Parameters separator document test is bad.
                        return l_bResult;
                    }

                    #endregion Parameters separator document test.

                    break;
            }

            #endregion TestParametersFonction

            //Tests are good.
            return l_bResult;
        }

        /// <summary>
        /// Test barcode function.
        /// </summary>
        /// <param name="p_lBarCodeId">BarCode Id</param>
        /// <param name="p_sJob"></param>
        private void SaveNewBarCodeFunctionTest(long p_lBarCodeId, string p_sJob)
        {
            //Variable initialization.
            Boolean l_bAddButtonIsEnablePropertie = IsEnabledAddBarCodeButton;
            Boolean l_bUpdateButtonIsEnablePropertie = IsEnabledUpdateBarCodeButton;
            BusinessEntitiesDomainContext l_oBEDomainContext = new BusinessEntitiesDomainContext();
            MessageBoxChildWindow L_oMessage = new MessageBoxChildWindow();

            try
            {
                //Test on the barcode.
                switch (p_lBarCodeId)
                {
                    case -1:
                        //BarCode Exists --> can't be save or modify.
                        L_oMessage = new MessageBoxChildWindow(String.Empty, string.Format(Resx.GetLabel(ResxRioManager.BARCODE_EXISTS), BarCodeIdentifierAdd).Replace("\\n", Environment.NewLine), MessageBoxButtons.Ok, MessageBoxIcon.Warning);
                        L_oMessage.Show();
                        L_oMessage.Closed += (s, ev) =>
                        {
                            IsEnabledAddBarCodeButton = l_bAddButtonIsEnablePropertie;
                            IsEnabledUpdateBarCodeButton = l_bUpdateButtonIsEnablePropertie;
                        };
                        break;

                    case 0:
                        //Barcode doesn't added. 
                        L_oMessage = new MessageBoxChildWindow(String.Empty, string.Format(Resx.GetLabel(ResxRioManager.BARCODE_ADD_ERROR), BarCodeIdentifierAdd).Replace("\\n", Environment.NewLine), MessageBoxButtons.Ok, MessageBoxIcon.Error);
                        L_oMessage.Show();
                        L_oMessage.Closed += (s, ev) =>
                        {
                            IsEnabledAddBarCodeButton = l_bAddButtonIsEnablePropertie;
                            IsEnabledUpdateBarCodeButton = l_bUpdateButtonIsEnablePropertie;
                        };
                        break;

                    default:
                        //Build new barcode function.
                        List<BarcodeSeparator> l_oLstBarCodeSeparator = BuiltBarCodeSeparatorList(p_lBarCodeId);

                        //Callback parameters in error case.                       
                        string l_sParameters = string.Format("{0};{1};{2};{3}", p_lBarCodeId, l_bAddButtonIsEnablePropertie, l_bUpdateButtonIsEnablePropertie, p_sJob);

                        //Save new barcode function.                        
                        string l_sListSerializableBarCodeSep = SerializationHelper.Serialize<List<BarcodeSeparator>>(l_oLstBarCodeSeparator);
                        var l_oQuery = l_oBEDomainContext.AddBarcodeSeparatorListQuery(l_sListSerializableBarCodeSep);
                        l_oBEDomainContext.Load(l_oQuery, OnAddBarCodeSeparatorListQueryCallback, l_sParameters);

                        break;
                }
            }
            catch (Exception ex)
            {
                //Properties value as origine.
                IsEnabledAddBarCodeButton = l_bAddButtonIsEnablePropertie;
                IsEnabledUpdateBarCodeButton = l_bUpdateButtonIsEnablePropertie;

                //Error.
                L_oMessage = new MessageBoxChildWindow(String.Empty, string.Format("{0}{1}{2}", string.Format(Resx.GetLabel(ResxRioManager.BARCODE_ADD_ERROR), BarCodeIdentifierAdd), Environment.NewLine, ex.Message), MessageBoxButtons.Ok, MessageBoxIcon.Error);
                L_oMessage.Show();
            }
        }

        /// <summary>
        /// Delete the BarCode.
        /// </summary>
        /// <param name="p_lBarCodeID">BarCode Id to delete</param>
        /// <param name="p_bMessageErrorAddBarCode">User message.</param>
        public void DeleteBarCode(long p_lBarCodeID, Boolean p_bMessageErrorAddBarCode)
        {
            //Delete barcode Id.
            BusinessEntitiesDomainContext l_oBEDomainContext = new BusinessEntitiesDomainContext();
            l_oBEDomainContext.DeleteBarcode(p_lBarCodeID, result =>
                                             {
                                                 if (result.HasError)
                                                 {
                                                     if (p_bMessageErrorAddBarCode == false)
                                                     {
                                                         //Error.
                                                         MessageBoxChildWindow L_oMessage = new MessageBoxChildWindow(String.Empty, string.Format("{0}{1}{2}", Resx.GetLabel(ResxRioManager.BARCODE_DELETE_ERROR), Environment.NewLine, result.Error.Message), MessageBoxButtons.Ok, MessageBoxIcon.Error);
                                                         L_oMessage.Show();
                                                     }
                                                 }
                                                 else if (p_bMessageErrorAddBarCode == false)
                                                 {
                                                     //Succes.
                                                     MessageBoxChildWindow L_oMessage = new MessageBoxChildWindow(String.Empty, Resx.GetLabel(ResxRioManager.BARCODE_DELETE_SUCCES), MessageBoxButtons.Ok, MessageBoxIcon.Information);
                                                     L_oMessage.Show();
                                                 }
                                                 else
                                                 {
                                                     //Error in barcode adding.
                                                     MessageBoxChildWindow L_oMessage = new MessageBoxChildWindow(String.Empty, string.Format(Resx.GetLabel(ResxRioManager.BARCODE_ADD_ERROR), BarCodeIdentifierAdd).Replace("\\n", Environment.NewLine), MessageBoxButtons.Ok, MessageBoxIcon.Error);
                                                     L_oMessage.Show();
                                                 }
                                             },
                                             null);
        }

        /// <summary>Built update barcode</summary>
        /// <returns>A Barcode to Update</returns>
        public Barcode BuiltBarCode(long p_lCabIdForUpdate = 0)
        {
            //Variable initialization
            Barcode l_oNewBarCodeToUpdate = new Barcode();

            try
            {
                l_oNewBarCodeToUpdate.Identifier = p_lCabIdForUpdate;
                l_oNewBarCodeToUpdate.ParamCode = BarCodeIdentifierAdd;
                l_oNewBarCodeToUpdate.Name = BarCodeNameAdd;
                l_oNewBarCodeToUpdate.Description = BarCodeDescriptionAdd;
                l_oNewBarCodeToUpdate.Enabled = ChbActiveBarCodeIsChecked;
                l_oNewBarCodeToUpdate.Type = SelectedDisplayBarCodeTypologyType.Value;
                l_oNewBarCodeToUpdate.Prefix = BarCodeTypologyPrefixAdd;
                l_oNewBarCodeToUpdate.Function = SelectedDisplayBarCodeFunction.Value;

                //Return Value.
                return l_oNewBarCodeToUpdate;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>Built update barcode separator</summary>
        /// <returns>A Barcode separator to Update</returns>
        public List<BarcodeSeparator> BuiltBarCodeSeparatorList(long p_lCabIdForSave)
        {
            //Initialization variable.
            List<BarcodeSeparator> l_oLstBarCodeSeparatorToUpdate = new List<BarcodeSeparator>();

            try
            {
                //Build new barcode function.
                switch (SelectedDisplayBarCodeFunction.Value)
                {
                    case BarcodeSeparatorType.DocumentSeparator:
                        //Build new barcode document separator list.                                
                        foreach (BarcodeDocumentSeparator l_oBarCodeSeparatorTemp in BarCodeSeparatorDocumentParametersList)
                        {
                            BarcodeDocumentSeparator l_oBarCodeDocumentSeparator = new BarcodeDocumentSeparator();
                            l_oBarCodeDocumentSeparator.CabIdentifier = p_lCabIdForSave;
                            l_oBarCodeDocumentSeparator.ParamCode = l_oBarCodeSeparatorTemp.ParamCode;
                            l_oBarCodeDocumentSeparator.OrganizationId = SelectedOrganization.Identifier;
                            l_oBarCodeDocumentSeparator.FolderTypeId = SelectedFolderType.Identifier;
                            if (l_oBarCodeSeparatorTemp.ItemName != null)
                            {
                                if (l_oBarCodeSeparatorTemp.ItemName != "")
                                {
                                    l_oBarCodeDocumentSeparator.ItemIdentifier = (from l_lItemIdentifier in BarCodeItemList
                                                                                  where l_lItemIdentifier.Name == l_oBarCodeSeparatorTemp.ItemName
                                                                                  select l_lItemIdentifier.Value).First();
                                }
                            }
                            l_oBarCodeDocumentSeparator.PdfMerge = l_oBarCodeSeparatorTemp.PdfMerge;
                            l_oBarCodeDocumentSeparator.DeleteBarCodePage = l_oBarCodeSeparatorTemp.DeleteBarCodePage;
                            l_oBarCodeDocumentSeparator.ErrorManagment = l_oBarCodeSeparatorTemp.ErrorManagment;
                            if ((l_oBarCodeSeparatorTemp.InfLimit == 0) && (l_oBarCodeSeparatorTemp.SupLimit == 0))
                            {
                                l_oBarCodeDocumentSeparator.DocLimit = false;
                            }
                            else
                            {
                                l_oBarCodeDocumentSeparator.DocLimit = true;
                            }
                            l_oBarCodeDocumentSeparator.InfLimit = l_oBarCodeSeparatorTemp.InfLimit;
                            l_oBarCodeDocumentSeparator.SupLimit = l_oBarCodeSeparatorTemp.SupLimit;
                            l_oBarCodeDocumentSeparator.NatureName = l_oBarCodeSeparatorTemp.NatureName;
                            l_oBarCodeDocumentSeparator.NatureIdentifier = l_oBarCodeSeparatorTemp.NatureIdentifier;
                            l_oBarCodeDocumentSeparator.TypeOfBarcode = SelectedDisplayBarCodeFunction.Value;

                            //Add to list.
                            l_oLstBarCodeSeparatorToUpdate.Add(l_oBarCodeDocumentSeparator);
                        }
                        break;

                    case BarcodeSeparatorType.FolderSeparator:
                        //Build new barcode folder separator list.
                        BarcodeFolderSeparator l_oBarCodeFolderSeparator = new BarcodeFolderSeparator();
                        l_oBarCodeFolderSeparator.CabIdentifier = p_lCabIdForSave;
                        l_oBarCodeFolderSeparator.DeleteBarCodePage = ChbDeleteBarCodePageIsChecked;
                        l_oBarCodeFolderSeparator.ErrorManagment = ChbManageErrorIsChecked;
                        l_oBarCodeFolderSeparator.CorrespondenceFolder = ChbFolderCorrespondenceIsChecked;
                        if (ChbFolderCorrespondenceIsChecked)
                        {
                            l_oBarCodeFolderSeparator.OrganizationId = SelectedOrganization.Identifier;
                            l_oBarCodeFolderSeparator.FolderTypeId = SelectedFolderType.Identifier;
                            l_oBarCodeFolderSeparator.UniqueIndexIdentifier = SelectedDisplayBarCodeSingleIndex.Value;
                            l_oBarCodeFolderSeparator.StartChar = BarcodeStartCharacterSingleIndex;
                            l_oBarCodeFolderSeparator.EndChar = BarcodeEndCharacterSingleIndex;
                        }
                        l_oBarCodeFolderSeparator.DocLimit = ChbLimitIsChecked;
                        if (ChbLimitIsChecked)
                        {
                            l_oBarCodeFolderSeparator.InfLimit = BarcodeLowerBoundLimit;
                            l_oBarCodeFolderSeparator.SupLimit = BarcodeUpperBoundLimit;
                            l_oBarCodeFolderSeparator.PageLimit = BtnRadPageLimitIsChecked;
                            l_oBarCodeFolderSeparator.DocumentLimit = BtnRadDocumentLimitIsChecked;
                        }
                        l_oBarCodeFolderSeparator.TypeOfBarcode = SelectedDisplayBarCodeFunction.Value;

                        //Add to list.
                        l_oLstBarCodeSeparatorToUpdate.Add(l_oBarCodeFolderSeparator);

                        break;
                }

                //Return Value.
                return l_oLstBarCodeSeparatorToUpdate;
            }
            catch (Exception)
            {
                return null;
            }
        }

        #endregion Methods

        #region Commands

        /// <summary>
        /// Saves the new BarCode.
        /// </summary>
        public void SaveNewBarCode()
        {
            //Variable initialization.
            Boolean l_oAddButtonIsEnablePropertie = IsEnabledAddBarCodeButton;
            Boolean l_oUpdateButtonIsEnablePropertie = IsEnabledUpdateBarCodeButton;

            //Test before the new barcode save.
            if (TestBeforeAddOrUpdateBarCode())
            {
                //Build new barcode to save generaly information.
                Barcode l_oNewBarCodeToSave = BuiltBarCode();

                //Save general information barcode if not exist.
                BusinessEntitiesDomainContext l_oBEDomainContext = new BusinessEntitiesDomainContext();
                l_oBEDomainContext.AddBarcode(l_oNewBarCodeToSave, result =>
                                              {
                                                  if (result.HasError)
                                                  {
                                                      //Properties are as the begining.
                                                      IsEnabledAddBarCodeButton = l_oAddButtonIsEnablePropertie;
                                                      IsEnabledUpdateBarCodeButton = l_oUpdateButtonIsEnablePropertie;

                                                      //Delete Barcode if is necessary.
                                                      DeleteBarCode(result.Value.Value, true);
                                                  }
                                                  else
                                                  {
                                                      SaveNewBarCodeFunctionTest(result.Value.Value, "SaveNewBarcode");
                                                  }
                                              },
                                              null);
            }
        }

        /// <summary>
        /// Update an BarCode.
        /// </summary>
        public void UpdateAnBarCode()
        {
            //Variable initialization.
            Boolean l_bAddButtonIsEnablePropertie = IsEnabledAddBarCodeButton;
            Boolean l_bUpdateButtonIsEnablePropertie = IsEnabledUpdateBarCodeButton;
            Barcode l_oBarcodeUpdate = new Barcode();
            //List<BarcodeSeparator> l_oBarcodeSeparatorLstToUpdate = new List<BarcodeSeparator>();

            //Differents test before start barcode updating.
            if (TestBeforeAddOrUpdateBarCode())
            {
                //Update Barcode selected.
                l_oBarcodeUpdate = BuiltBarCode(BarCodeInformationToUpdate[0].Identifier);

                //Update barcode.
                BusinessEntitiesDomainContext l_oBEDomainContext = new BusinessEntitiesDomainContext();
                l_oBEDomainContext.UpdateBarcode(l_oBarcodeUpdate, result =>
                {
                    if (result.HasError)
                    {
                        //Properties are as the begining.
                        IsEnabledAddBarCodeButton = l_bAddButtonIsEnablePropertie;
                        IsEnabledUpdateBarCodeButton = l_bUpdateButtonIsEnablePropertie;

                        //Delete Barcode if is necessary.
                        DeleteBarCode(result.Value.Value, true);
                    }
                    else
                    {
                        if ((IsModifyMode) && (result.Value.Value != 0) && (result.Value.Value != -1))
                        {
                            SaveNewBarCodeFunctionTest(BarCodeInformationToUpdate[0].Identifier, "UpdateNewBarcode");
                        }
                        else
                        {
                            SaveNewBarCodeFunctionTest(result.Value.Value, "UpdateNewBarcode");
                        }
                    }
                },
                null);
            }
        }

        /// <summary>
        /// Closes the add BarCode windows.
        /// </summary>
        public void CloseAddBarCode()
        {
            //Close the add BarCode windows.
            CloseTab();
        }

        #endregion Commands

        #region CallBack

        /// <summary>
        /// AddBarCodeSeparatorList Callback
        /// </summary>
        /// <param name="result">The result.</param>
        public void OnAddBarCodeSeparatorListQueryCallback(LoadOperation<ReturnObject> result)
        {
            //Variable initialization.
            if (result.UserState == null) return;
            string l_sParametersCallBack = (string)result.UserState;
            string[] l_sParameters = l_sParametersCallBack.Split(new[] { ';' });
            string l_sUserMessage = string.Empty;

            if (result.HasError)
            {
                //Error in recording. 

                //Properties are as a begining.
                IsEnabledAddBarCodeButton = Convert.ToBoolean(l_sParameters[1]);
                IsEnabledUpdateBarCodeButton = Convert.ToBoolean(l_sParameters[2]);

                //Delete barcode save.
                DeleteBarCode(Convert.ToInt64(l_sParameters[0]), true);
            }
            else
            {
                switch (l_sParameters[3])
                {
                    case "SaveNewBarcode":
                        l_sUserMessage = string.Format(Resx.GetLabel(ResxRioManager.BARCODE_ADD_SUCCES), BarCodeIdentifierAdd);
                        break;
                    case "UpdateNewBarcode":
                        l_sUserMessage = string.Format(Resx.GetLabel(ResxRioManager.BARCODE_UPDATE_SUCCES), BarCodeIdentifierAdd);
                        break;
                }

                //Send message to refresh consultation barcode if this table is open.
                NeedToRefreshBarCodeConsultationList();

                //User succesfull message. 
                MessageBoxChildWindow L_oMessage = new MessageBoxChildWindow(String.Empty, l_sUserMessage, MessageBoxButtons.Ok, MessageBoxIcon.Information);
                L_oMessage.Show();
                L_oMessage.Closed += (s, ev) => CloseTab();
            }
        }

        #endregion CallBack
    }
}